package dj.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dj.dao.BaseDao;
import dj.service.BaseService;

@Service("baseService")
public class BaseServiceImpl<T> implements BaseService<T> {

	private BaseDao<T> baseDao;

	public BaseDao<T> getBaseDao() {
		return baseDao;
	}

	@Autowired
	public void setBaseDao(BaseDao<T> baseDao) {
		this.baseDao = baseDao;
	}

	public List<T> getObjcetPagination(Class<T> clazz, Map<String, Object> params, Map<String, String> conditions, int page, int rows, String order) {
		List<T> objects = new ArrayList<T>();

		String hql = "from " + clazz.getName() + " where 1=1";
		if (null != params && params.size() > 0) {
			for (String key : params.keySet()) {
				hql += " and " + key + " " + conditions.get(key) + ":" + key + " ";
			}
			if (null != order && order.indexOf("order") > -1) {
				hql += order;
			}

			objects = baseDao.find(hql, params, page, rows);
		} else {
			if (null != order && order.indexOf("order") > -1) {
				hql += order;
			}
			objects = baseDao.find(hql, page, rows);
		}

		return objects;
	}

	public Long getObjectNum(Class<T> clazz, Map<String, Object> params, Map<String, String> conditions) {
		String hql = "select count(*) from " + clazz.getName() + " where 1=1";
		Long objNum = null;
		if (null != params && params.size() > 0) {
			for (String key : params.keySet()) {
				hql += " and " + key + " " + conditions.get(key) + ":" + key + " ";
			}
			objNum = baseDao.getCount(hql, params);
		} else {
			objNum = baseDao.getCount(hql);
		}
		return objNum;
	}
	
	

}
